package application;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

public class Character {
	
	public String Name;
	public String NickName;
	public String FirstName;
	public String Description;
	public String Age;
	public String Gender;
	public String Image;
	public String Role;
	public String Level;
	public String Faction;
	
	public Character(String Name, String NickName, 
			String FirstName, String Description, String Age,
			String Gender, String Image, String Role, String Level,String Faction)
	{
		super();
		this.Name = Name;
		this.NickName = NickName;
		this.FirstName = FirstName;
		this.Description = Description;
		this.Age = Age;
		this.Gender = Gender;
		this.Image = Image;
		this.Role = Role;
		this.Level = Level;
		this.Faction = null;
	}
	
	public Character()
	{
		super();
		this.Name = null;
		this.NickName = null;
		this.FirstName = null;
		this.Description = "unknown character";
		this.Age = "18";
		this.Gender = "unknown";
		this.Image = "no image";
		this.Role = "Main Cast";
		this.Level = null;
		this.Faction = null;
	}
	
	public void dbCreateCharacter (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

	        		db.store(this);


		}
		finally{
			db.close();
		}
	}
	
	public void dbEditCharacter (String dbFile, Character c){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Character> result = db
		        .queryByExample(Character.this);
				Character found = (Character) result.next();
				db.delete(found);
				this.setAge(c.getAge());
				this.setDescription(c.getDescription());
				this.setFaction(c.getFaction());
				this.setFirstName(c.getFirstName());
				this.setGender(c.getGender());
				this.setImage(c.getImage());
				this.setLevel(c.getLevel());
				this.setName(c.getName());
				this.setNickName(c.getNickName());
				this.setRole(c.getRole());
				db.store(this);
				
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}
	
	public void dbDeleteCharacter (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Character> result = db
		        .queryByExample(Character.this);
				Character found = (Character) result.next();
				db.delete(found);
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}

	public String getName() {
		return Name;
	}

	public String getNickName() {
		return NickName;
	}

	public String getFirstName() {
		return FirstName;
	}

	public String getDescription() {
		return Description;
	}

	public String getAge() {
		return Age;
	}

	public String getGender() {
		return Gender;
	}

	public String getImage() {
		return Image;
	}

	public String getRole() {
		return Role;
	}

	public String getLevel() {
		return Level;
	}

	public void setName(String name) {
		Name = name;
	}

	public void setNickName(String nickName) {
		NickName = nickName;
	}

	public void setFirstName(String firstName) {
		FirstName = firstName;
	}

	public void setDescription(String description) {
		Description = description;
	}

	public void setAge(String age) {
		Age = age;
	}

	public void setGender(String gender) {
		Gender = gender;
	}

	public void setImage(String image) {
		Image = image;
	}

	public void setRole(String role) {
		Role = role;
	}

	public void setLevel(String level) {
		Level = level;
	}
	
	public String toString(){
		return FirstName + " '" + NickName + "' " + Name;
	}

	public String getFaction() {
		return Faction;
	}

	public void setFaction(String faction) {
		Faction = faction;
	}



}
